package com.ld.service.impl;

import com.ld.mapper.UsersMapper;
import com.ld.pojo.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;


@Service
public class MyUserDetailService implements UserDetailsService {

	@Autowired
	private UsersMapper usersMapper;
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		System.out.println("username = " + username);
		//1. 根据用户名查询出用户
		Users user = new Users();
		user.setUsername(username);
		Users users = usersMapper.selectOne(user);
		//2. 如果不存在，就抛出异常
		if(users == null){
			throw new UsernameNotFoundException("用户名不存在！");
		}

		return new User(username,users.getPassword(), AuthorityUtils.commaSeparatedStringToAuthorityList("role,userxx,select,admin,ROLE_user,ROLE_admin"));
	}
}
